package cn.com.pxto.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.com.pxto.commons.StringUtils;
import cn.com.pxto.model.ExcelGuest;
import cn.com.pxto.model.Guest;
import cn.com.pxto.model.LessonType;
import cn.com.pxto.model.School;
import cn.com.pxto.service.GuestManager;
import cn.com.pxto.service.SchoolManager;
import cn.com.pxto.web.excel.CreatExcel;

public class DownloadExcelAction extends DispatchAction {
	
	private GuestManager guestManager ;
	private SchoolManager schoolManager;

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		@SuppressWarnings("unused")
		List<Guest> guestList=guestManager.listGuestForDownload();
		List<School> schoolList = schoolManager.listAllSchool();
		List<LessonType> lessonTypeList = schoolManager.listAllLessonType();
		Map<Integer, String> schoolMap = schoolListToMap(schoolList); 
		Map<Integer, String> ltMap = lessonTypeListToMap(lessonTypeList); 
//		guestToDownloadGuest(guestList,schoolMap);
		List<ExcelGuest> eglist = guestToDownloadGuest(guestList,schoolMap,ltMap);
		CreatExcel ce = new CreatExcel();
		ce.createUserExcel(eglist,response);
		return null;
	}

	private List<ExcelGuest> guestToDownloadGuest(List<Guest> guestList,Map<Integer, String> schoolMap,Map<Integer, String> ltMap){
		List<ExcelGuest> eglist = new ArrayList<ExcelGuest>();
		Iterator<Guest> it = guestList.iterator();
		while(it.hasNext()){
			Guest guest = it.next();
			String schoolName = schoolMap.get(guest.getSchoolId());
			String lessonTypeName = ltMap.get(guest.getLessonTypeId());
			ExcelGuest exg = new ExcelGuest(guest,schoolName);
			exg.setType(lessonTypeName);
			String temp = StringUtils.long2DateString(guest.getLogonDate());
			exg.setTime(temp);
			eglist.add(exg);
		}
		return eglist;
	}
	
	public Map<Integer, String> lessonTypeListToMap(List<LessonType> lessonTypeList){
		Map<Integer ,String >  lessontTypeMap = new HashMap<Integer, String>();
		Iterator<LessonType> it = lessonTypeList.iterator();
		while(it.hasNext()){
			LessonType lt = it.next();
			lessontTypeMap.put(lt.getLtid(), lt.getName());
		}
		return lessontTypeMap;
	}
	
	public Map<Integer, String> schoolListToMap(List<School> schoolList){
		Map<Integer ,String >  schoolMap = new HashMap<Integer, String>();
		Iterator<School> it = schoolList.iterator();
		while(it.hasNext()){
			School school = it.next();
			schoolMap.put(school.getSid(), school.getName());
		}
		return schoolMap;
	}
	
	public void setGuestManager(GuestManager guestManager) {
		this.guestManager = guestManager;
	}

	public void setSchoolManager(SchoolManager schoolManager) {
		this.schoolManager = schoolManager;
	}
	
}
